package com.hong.service_cart.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hong.fy_common_commomutil.common.result.Result;
import com.hong.fy_model.model.cart.CartEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.hong.service_cart.service.CartService;


import java.util.List;



/**
 * 
 *
 * @author wqh
 * @email 1601709391@qq.com
 * @date 2022-06-16 21:42:56
 */
@RestController
@RequestMapping("/cart")
@Api(tags = "购物车")
public class CartController {

    @Autowired
    private CartService cartService;



    @ApiOperation(value = "新增购物车")
    @PostMapping("/addCart/{userId}")
    public CartEntity add(@PathVariable("userId") @ApiParam("用户ID") Integer userId){
        CartEntity cart = new CartEntity();
        cart.setUserId(userId); //设置用户ID

        //根据用户ID查询购物车，如果没有则新增，如果有则不新增，一个用户只能有一个购物车
        LambdaQueryWrapper<CartEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(CartEntity::getUserId, userId);
        CartEntity cartEntity = cartService.getOne(queryWrapper);
        if (cartEntity == null){
            cartService.save(cart);
            return cart;
        }else {
            return null;
        }


    }
//
//    @ApiOperation(value = "删除")
//    @DeleteMapping("/delete/{cartId}")
//    public Result delete(@PathVariable Integer cartId){
//        boolean b = cartService.removeById(cartId);
//        if (b) {
//            return Result.ok("删除成功");
//        }else {
//            return Result.fail("删除失败");
//        }
//    }
//
//    @ApiOperation(value = "更新")
//    @PutMapping("cart/update")
//
//    public Result update(@RequestBody CartEntity cart){
//        cartService.updateById(cart);
//        return Result.ok();
//    }

//    @ApiOperation(value = "查询分页数据")
//    @PostMapping("cart/listByPage")

//    public Result findListByPage(@RequestBody CartEntity cart){
//        Page page = new Page(cart.getPage(), cart.getLimit());
//        LambdaQueryWrapper<CartEntity> queryWrapper = Wrappers.lambdaQuery();
//        //查询条件示例
//        //queryWrapper.eq(CartEntity::getId, cart.getId());
//        IPage<CartEntity> iPage = cartService.page(page, queryWrapper);
//        return DataResult.success(iPage);
//    }

}
